import Vue from 'vue'
import Vuex from 'vuex'
import axios from "axios"
import Home from "./home/home.js"
Vue.use(Vuex)

export default new Vuex.Store({
   // state是放变量的，即存放数据的
  state: {
    num:1,
    arr:[],
    name:"1909a",
    carArr:[],

  },
  // 放同步方法的，也是修改state里定义数据的唯一途径
  mutations: {
      setNum(state,payload){
          console.log(payload)
          // state就是上面定义的state，payload是.vue传过来的值
          state.num = state.num + payload
      },
      setArr(state,payload){
        state.arr = payload
      },
      setName(state,payload){
        console.log(payload)
        state.name = payload
      },
      setCararr(state,payload){
        state.carArr = payload
      }
  },
  // action是放异步方法的，一般用于发送网络请求
  actions: {
      getList({commit,state},payload){
          console.log(payload)
        axios.get("/list").then(res=>{
            console.log(res.data)
            commit("setArr",res.data)
        })
      },
      getCarList({state,commit},paylaod){
          axios.get("https://baojia.chelun.com/v2-car-getMasterBrandList.html").then(res=>{
              console.log(res)
              commit("setCararr",res.data)
          })
        // 从action里可以结构出state，commit
        // commit 是用来触发mutation里的方法的
      }
  },
  // vuex模块化
  modules: {
    Home
  }
})
